﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Drawing;

namespace eCompany
{
    class ToolStripComboBoxExtended : ToolStripComboBox
    {

        public ImageList ImageList { get; set; }

        public ToolStripComboBoxExtended()
        {
            InitializeComponent();
        }

        public void InitializeComponent()
        {
            this.ComboBox.SuspendLayout();
            // 
            // ComboBoxExtended
            // 
            this.ComboBox.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.ComboBoxExtended_DrawItem);
            this.ComboBox.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;

            this.ComboBox.ResumeLayout(false);

        }

        private void ComboBoxExtended_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            //Get Image from imageList

            if (e.Index != -1)
            {
                ToolStripComboBoxExtendedItem item = (ToolStripComboBoxExtendedItem)this.Items[e.Index];
                Image img = ImageList.Images[item.ImageTag];

                if (img != null)
                {
                    //Draw the image in combo box using its bound, here size of image is            
                    // 10, 10 you can increase the size if you want            
                    e.Graphics.DrawImage(img, e.Bounds.X, e.Bounds.Y, this.ComboBox.ItemHeight, this.ComboBox.ItemHeight);
                    //we need to draw the item as string because we made drawmode to ownervariable            
                }
                StringFormat fmt = new StringFormat();
                fmt.LineAlignment = StringAlignment.Center;

                e.Graphics.DrawString(this.Items[e.Index].ToString(), this.Font,
                        System.Drawing.Brushes.Black,
                        new RectangleF(e.Bounds.X + this.ComboBox.ItemHeight, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), fmt);
                //draw rectangle over the item selected            
                e.DrawFocusRectangle();
            }
        }
    }

    public class ToolStripComboBoxExtendedItem
    {
        public Object Obj { get; set; }
        
        public string ImageTag { get; set; }

        public override string ToString()
        {
            return Obj.ToString();
        }
    }
}
